package com.iukcy.hight.controller;

import com.iukcy.hight.service.ClassService;
import com.iukcy.hight.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/file")
public class FileOptController {
    private final static Logger logger = LoggerFactory.getLogger(FileOptController.class);
    @Autowired
    ClassService classService;

    @RequestMapping("/index")
    public String index() {
        return "index";
    }


    @GetMapping("/multiUpload")
    public String multiUpload() {
        return "multiUpload";
    }

    @RequestMapping("/multiUpload")
    @ResponseBody
    public String multiUpload(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        String filePath = "/Users/zhangzhufu/upload/";
        for (int i = 0; i < files.size(); i++) {
            MultipartFile file = files.get(i);
            if (file.isEmpty()) {
                return "上传第" + (i + 1) + "个文件失败";
            }
            String fileName = file.getOriginalFilename();

            File dest = new File(filePath + fileName);
            try {
                file.transferTo(dest);
                logger.info("第" + (i + 1) + "个文件上传成功");
            } catch (IOException e) {
                logger.error(e.toString(), e);
                return "上传第" + (i++) + "个文件失败";
            }
        }

        return "上传成功";

    }

    @GetMapping("/upload")
    public String upload() {
        return "upload1";
    }

    @RequestMapping("/uploadFile")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        String fileName = file.getOriginalFilename();
        String filePath = "/Users/zhangzhufu/upload/";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            logger.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            logger.error(e.toString(), e);
        }
        return "上传失败！";
    }

    /**
     * <strong>方法描述 ：</strong> 下载<br/>
     * <strong>创建时间：</strong> 2020/9/22 3:06 下午<br/>
     *
     * @param [classId, userId]
     * @return com.iukcy.hight.util.Result<java.lang.String>
     */
    @GetMapping("/download")
    @ResponseBody
    public String download(@RequestParam Integer test, HttpServletResponse response) {
        String fileName = "class.xlsx";
        //使用流的形式下载文件
        try {
            int a = 5 / test;
            FileUtil.downloadFile(response, fileName);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return "文件下载出错";
        }
        return null;
    }

}
